using System;
using System.Data;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class CopyColumns : MissionPlugin
    {
        [Package(Direction.Input, "Defines the origin package where you get column-data from.")]
        enum OriginPackage { }

        [Package(Direction.Input, "Defines the columns you like to copy.")]
        enum ColumnsToCopy
        {
            [Column(typeof(string), "Name of the origin packageColumn you like to copy.")]
            OriginColumnName,
            [Column(typeof(string), "The new name of the packageColumn you like to copy from origin packageColumn ")]
            NewColumnName
        }

        [Package(Direction.Output, "The new package with all copied data.")]
        enum Output
        { }

        [Plugin(PluginDevStatus.Production, "Copy the expected data from a given package to a new package column by column.")]
        public CopyColumns()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        protected override void Work()
        {
            //copy the originPackage to the output
            MissionPluginPackage outputPackage = this.GetPackageByName(typeof(Output));
            outputPackage.CopyDataFromPackage(this.GetPackageByName(typeof(OriginPackage)));

            // it is very important to delete all nonrelevant columns, before we rename them!
            // so we avoid renaming a column, to an already allocated nonrelevant column
            int numColumns = outputPackage.Columns.Count;
            for (int columnID = 0; columnID < numColumns; columnID++)
            {                
                if (false == this.IsColumnToCopy(outputPackage.Columns[columnID].ColumnName) && true == outputPackage.Columns.CanRemove(outputPackage.Columns[columnID]))
                {
                    outputPackage.Columns.Remove(outputPackage.Columns[columnID]);
                    numColumns--;
                    columnID--;
                }
            }

            // rename all other column (if necessary)
            DataTableReader columnsToCopyPackage = this.GetDataReader(typeof(ColumnsToCopy));
            while (columnsToCopyPackage.Read())
            {
                string originColumnName = columnsToCopyPackage[ColumnsToCopy.OriginColumnName.ToString()].ToString();
                string newColumnName = columnsToCopyPackage[ColumnsToCopy.NewColumnName.ToString()].ToString();

                this.AddNewLoggingMessage(typeof(ColumnsToCopy), String.Format("copy colum {0} to:{1}", originColumnName, newColumnName));

                //rename the copied columns, if necessary
                if (false == String.IsNullOrEmpty(originColumnName))
                {
                    outputPackage.Columns[originColumnName].ColumnName = newColumnName;
                }
            }
        }

        private bool IsColumnToCopy(string columnName)
        {
            DataTableReader columnsToCopyPackageReader = this.GetDataReader(typeof(ColumnsToCopy));
            while (columnsToCopyPackageReader.Read())
            {
                
                
                if (columnName == columnsToCopyPackageReader[ColumnsToCopy.OriginColumnName.ToString()].ToString())
                {
                    return true;
                }
            }
            return false;
        }
    }
}
